/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.stat;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JDialog;
import jpcap.packet.Packet;
import org.jdesktop.swingx.JXTitledPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import tnviet.proj.jna.JNCaptor;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;

/**
 *
 * @author Administrator
 */
public class JNContinuousStatDialog extends JNStatDialog {

    JNStatisticsController controller;
    TimeSeries[] timeSeries;
    String[] labels;
    long[] lastValue;
    int statType;
    int count,currentCount=0;
    long currentSec=0;
    ResourceManager resourceManager = new ResourceManager(this.getClass(), Program.locale);

    public static JNContinuousStatDialog createDialog(Frame owner, Vector packets, JNStatisticsController controller){
        final JNContinuousStatDialog dialog = new JNContinuousStatDialog(owner,packets, controller,0);
        final JNCaptor captor = JNCaptor.getInstance();
        dialog.setVisible(true);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                captor.removeStatDialog(dialog);
            }
        });
        return dialog;
    }

    public JNContinuousStatDialog(Frame owner, Vector packets, JNStatisticsController _controller, int type) {
        super(owner, _controller.getName(), false);
        this.controller = _controller;
        controller.analyze(packets);
        this.statType = type;
        long[] ret = controller.getValues(statType);
        lastValue = new long[ret.length];
        System.arraycopy(ret, 0, lastValue, 0, lastValue.length);
        labels = controller.getLabels();
        timeSeries = new TimeSeries[labels.length];
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        for(int i=0; i<labels.length;i++){
            timeSeries[i] = new TimeSeries(labels[i], Second.class);
            timeSeries[i].setMaximumItemAge(120);
            dataset.addSeries(timeSeries[i]);
        }
        DateAxis domain = new DateAxis();
        NumberAxis range = new NumberAxis();
        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        XYPlot plot = new XYPlot(dataset, domain, range, renderer);
        domain.setLowerMargin(0.0);
        range.setLowerMargin(0.0);
        domain.setAutoRange(true);
        range.setAutoRange(true);
        JFreeChart chart = new JFreeChart(plot);
        chart.setTitle(controller.getStatTypes()[statType]);
        ChartPanel chartPanel = new ChartPanel(chart, true);
        chartPanel.setMouseZoomable(false, false);
        JXTitledPanel chartPane = new JXTitledPanel(resourceManager.getString("chart.title"), chartPanel);
        this.getContentPane().add(chartPane, BorderLayout.CENTER);
        this.pack();
        this.setLocationRelativeTo(null);
    }
    public void updateSeries(){
        long[] ret = controller.getValues(statType);
        long[] diff = new long[ret.length];
        for(int i=0; i<diff.length;i++){
            diff[i] = ret[i] - lastValue[i];
        }
        System.arraycopy(ret, 0, lastValue, 0, lastValue.length);
        for(int i=0; i<timeSeries.length;i++){
            timeSeries[i].addOrUpdate(new Second(), diff[i]);
        }
    }
    @Override
    void fireUpdate() {
        updateSeries();
    }

    @Override
    public void addPacket(Packet p) {
        controller.addPacket(p);
    }

    @Override
    public void clear() {
        controller.clear();
        for(int i=0; i<lastValue.length;i++){
            lastValue[i] = 0;
        }
    }

}
